﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CP.Entities;
using CP.DBService;
using CP.Utility;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace CP.Repositories.GPM
{
    public class ResourceAuthRepository
    {
        DBHelper db = DBHelper.GPMCreateInstance();

        public int ResourceAuth(int menuid,int webpartid, int resourceid, IList<int> groups)
        {
            return ResourceProc(menuid,webpartid, resourceid, groups, "gpm.proc_resource_auth");
        }

        public int ResourceUnAuth(int menuid,int webpartid, int resourceid, IList<int> groups)
        {
            return ResourceProc(menuid,webpartid, resourceid, groups, "gpm.proc_resource_unauth");
        }

        private int ResourceProc(int menuid,int webpartid, int resourceid, IList<int> groups, string procname)
        {
            string errmsg = string.Empty;

            try
            {
                OracleParameter[] parameters ={
                new OracleParameter("p_menuid",OracleDbType.Int32),
                new OracleParameter("p_webpartid",OracleDbType.Int32),
                new OracleParameter("p_resourceid",OracleDbType.Int32),
                new OracleParameter("p_groups",OracleDbType.Varchar2,4000),
                new OracleParameter("p_errmsg",OracleDbType.Varchar2,200)
　　　　　　　　　　　　　};
                parameters[0].Direction = ParameterDirection.Input;
                parameters[1].Direction = ParameterDirection.Input;
                parameters[2].Direction = ParameterDirection.Input;
                parameters[3].Direction = ParameterDirection.Input;
                parameters[4].Direction = ParameterDirection.Output;

                parameters[0].Value = menuid;
                parameters[1].Value = webpartid;
                parameters[2].Value = resourceid;
                parameters[3].Value = string.Join(",", groups.ToArray());

                db.ExecuteNonQuery(procname, parameters, CommandType.StoredProcedure);
                errmsg = parameters[4].Value.ToString();
                if (string.IsNullOrEmpty(errmsg))
                    return 1;
                throw new Exception(errmsg);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public int MenuRightCopy(int menufrom, int menuto)
        {
            string errmsg = string.Empty;

            try
            {
                OracleParameter[] parameters ={
                new OracleParameter("p_menufrom",OracleDbType.Int32),
                new OracleParameter("p_menuto",OracleDbType.Int32), 
                new OracleParameter("p_errmsg",OracleDbType.Varchar2,200)
　　　　　　　　　　　　　};
                parameters[0].Direction = ParameterDirection.Input;
                parameters[1].Direction = ParameterDirection.Input; 
                parameters[2].Direction = ParameterDirection.Output;

                parameters[0].Value = menufrom;
                parameters[1].Value = menuto; 

                db.ExecuteNonQuery("gpm.proc_menu_right_copy", parameters, CommandType.StoredProcedure);
                errmsg = parameters[2].Value.ToString();
                if (string.IsNullOrEmpty(errmsg))
                    return 1;
                throw new Exception(errmsg);
            }
            catch (Exception e)
            {
                throw e;
            }
        } 
    }
}
